
package kaizone.haining.chaobohui.widget;

import java.util.List;

import kaizone.haining.chaobohui.R;
import kaizone.haining.chaobohui.activity.CBHActivity;
import kaizone.haining.chaobohui.activity.DetailActivity;
import kaizone.haining.chaobohui.args.AppArgs;
import kaizone.haining.chaobohui.json.Banner;
import kaizone.haining.chaobohui.json.Banner.PicModel;
import kaizone.haining.chaobohui.json.BannerDO;
import kaizone.haining.chaobohui.util.ImageFetcher;
import kaizone.haining.chaobohui.util.Utils;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewParent;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AdView extends FrameLayout {

    private static int mShowSmallIndex = 0;

    private static int mShowBottomIndex = 0;

    private static int mShowBigIndex = 0;

    private String mStateFlag;

    private TextView mCloseView;

    private ImageView mImageView;

    private ImageFetcher mImageFetcher;

    public AdView(Context context) {
        super(context);
        init();
    }

    public AdView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        initImageFetcher();
        this.setClickable(true);
        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                openAdHandler();
            }
        });
    }

    public void openAdHandler() {
        if (mStateFlag.equals(Banner.BIG)) {
            openAdBig();
        }//
        else if (mStateFlag.equals(Banner.SMALL)) {
            openAdSmall();
        }//
        else if (mStateFlag.equals(Banner.BOTTOM)) {
            openAdBottom();
        }
    }

    private void initImageFetcher() {
        if (getContext() instanceof CBHActivity) {
            mImageFetcher = ((CBHActivity) getContext()).getImageFetcher();
        }//
        else if (getContext() instanceof DetailActivity) {
            mImageFetcher = ((DetailActivity) getContext()).getImageFetcher();
        }
    }

    public void openAdSmall() {
        openAd(Banner.SMALL, mShowSmallIndex);
    }

    public void openAdBig() {
        openAd(Banner.BIG, mShowBigIndex);
    }

    public void openAdBottom() {
        openAd(Banner.BOTTOM, mShowBottomIndex);
    }

    public void openAd(String flag, int index) {
        final BannerDO bannerDO = AppArgs.sBannerDO;
        final Banner banner = bannerDO.getBanner(flag);
        final List<PicModel> picModels = banner.getPicModels();
        if (picModels == null || picModels.isEmpty())
            return;
        int count = picModels.size();
        if (index >= count) {
            index = 0;
        }
        PicModel pm = picModels.get(index);
        if (pm == null)
            return;
        Utils.openBrowser(getContext(), pm.clickUrl);
    }

    public void loadAdSmall() {
        mShowSmallIndex = loadAd(Banner.SMALL, mShowSmallIndex);
        mShowSmallIndex++;

    }

    public void loadAdBottom() {
        mShowBottomIndex = loadAd(Banner.BOTTOM, mShowBottomIndex);
        mShowBottomIndex++;
    }

    public void loadAdBig() {
        mCloseView.setVisibility(View.GONE);
        mShowBigIndex = loadAd(Banner.BIG, mShowBigIndex);
        mShowBigIndex++;
    }

    public int loadAd(String flag, int index) {
        mStateFlag = flag;
        final BannerDO bannerDO = AppArgs.sBannerDO;
        if (bannerDO == null)
            return 0;
        final Banner banner = bannerDO.getBanner(flag);
        if (banner == null)
            return 0;
        final List<PicModel> picModels = banner.getPicModels();
        if (picModels == null || picModels.isEmpty())
            return 0;
        int count = picModels.size();
        if (index >= count) {
            index = 0;
        }
        PicModel pm = picModels.get(index);
        if (pm == null)
            return 0;
        mImageView.setVisibility(View.VISIBLE);
        if (mStateFlag.equals(Banner.BIG)) {
            mCloseView.setVisibility(View.GONE);
        } else {
            mCloseView.setVisibility(View.VISIBLE);
        }
        mImageFetcher.loadImage(pm.picUrl, mImageView);
        return index;
    }

    @Override
    protected void onFinishInflate() {
        mCloseView = (TextView) findViewById(R.id.ad_img);
        Drawable drawable = Utils.scaleDrawable(getContext(), R.drawable.closead, 0.6f);
        mCloseView.setCompoundDrawables(null, null, drawable, null);
        mCloseView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                hide();
            }
        });
        mImageView = (ImageView) findViewById(R.id.ad_img01);
        mImageView.setScaleType(ScaleType.FIT_XY);
    }

    private void hide() {
        this.setVisibility(View.GONE);
    }

    public void rangeWithPanel(Activity activity) {
        final DisplayMetrics displayMetrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        final int screenwidth = displayMetrics.widthPixels;
        final int screenHeight = displayMetrics.heightPixels;

        final ViewParent parent = this.getParent();
        if (parent instanceof LinearLayout) {
            // final int width = ((LinearLayout) parent).getWidth();
            final int height = screenwidth * 1 / 6;
            this.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, height));
        }
    }

    public void fullScreen(Activity activity) {
        final DisplayMetrics displayMetrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        final int screenWidth = displayMetrics.widthPixels;
        final int screenHeight = displayMetrics.heightPixels;

        final ViewParent parent = this.getParent();
        if (parent instanceof LinearLayout) {
            // final int width = ((LinearLayout) parent).getWidth();;
            this.setLayoutParams(new LinearLayout.LayoutParams(screenWidth, screenHeight));
        }//
        else if (parent instanceof FrameLayout) {
            this.setLayoutParams(new FrameLayout.LayoutParams(screenWidth, screenHeight));
        }
    }

    public void setAdImageResource(int resid) {
        this.setBackgroundResource(resid);
    }
}
